﻿using System;
using System.Windows;
using System.Windows.Controls;
using SoftSource.Common;

namespace SoftSource.Controls
{
    public partial class DateRangeSelector : UserControl
    {
        static Func<object> getCurrent;

        public DateRangeSelector()
        {
            InitializeComponent();
            getCurrent = () => this;
            Loaded += new RoutedEventHandler(DateRangeSelector_Loaded);
        }

        void DateRangeSelector_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = this;
        }

        private void WeekCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            this.StartDate = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);
            this.EndDate = this.StartDate.AddDays(7);

            this.MonthCheckBox.IsChecked = false;
            this.YearCheckBox.IsChecked = false;
        }

        private void MonthCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            var now = DateTime.Now;
            this.StartDate = new DateTime(now.Year, now.Month, 1);
            this.EndDate = this.StartDate.AddMonths(1).AddDays(-1);

            this.WeekCheckBox.IsChecked = false;
            this.YearCheckBox.IsChecked = false;
        }

        private void YearCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            var now = DateTime.Now;
            this.StartDate = new DateTime(now.Year, 1, 1);
            this.EndDate = this.StartDate.AddYears(1).AddDays(-1);

            this.WeekCheckBox.IsChecked = false;
            this.MonthCheckBox.IsChecked = false;
        }

        public DateTime StartDate
        {
            get { return (DateTime)GetValue(StartDateProperty); }
            set { SetValue(StartDateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StartDate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StartDateProperty =
            DependencyProperty.Register("StartDate", typeof(DateTime), typeof(DateRangeSelector), new PropertyMetadata(DateTime.Now.AddMonths(-1), (sender, e) =>
                {
                    var x = e.NewValue;
                }));


        public DateTime EndDate
        {
            get { return (DateTime)GetValue(EndDateProperty); }
            set { SetValue(EndDateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for EndDate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EndDateProperty =
            DependencyProperty.Register("EndDate", typeof(DateTime), typeof(DateRangeSelector), new PropertyMetadata(DateTime.Now));


    }
}
